Ripat DBA      EN   Contact  

SQL articles.
Last changed: 04 May 2024

How to get the free disk space of a remote server?


I will discuss two methods to get the free disk space of a remote SQL server.

1. PowerShell
2. SQL job



1. PowerShell

Use a PowerShell file to collect the free diskspace. This method also requires the SqlServer module and DBATools to be installed.

Steps to do:
- create the database 'ServerViewer' and the table 'Drives', see Database and Extra tables
- create a new database with the name 'ServerViewerMaintenance'
- create a new table 'DrivesPSStaging' in the database 'ServerViewerMaintenance'
- create the PowerShell file get_diskspace_servername.ps1
- create a credential and a proxyaccount (SQLProxy)
- create a job with a jobstep that runs the PowerShell file with the proxyaccount


Create a table 'DrivesPSStaging'.





Create the PowerShell file get_diskspace_servername.ps1 and place the file in the folder D:\Apps\PS\.





Create a job with a jobstep that runs the PowerShell file every day with a proxyaccount.

CmdExec
command=powershell.exe "D:\Apps\PS\get_diskspace_servername.ps1"
proxy_name=SQLProxy



2. SQL job

Use a SQL job that starts a stored procedure that gets the diskspace. This method requires a table and a stored procedure and a job on every remote server.

Steps to do:
- create the table 'Drives' in the master database on the remote server
- create the stored procedure 'sp_diskspace' in the master database on the remote server
- create a job '#dba - Drives' on the remote server
- retrieve the diskinfo from each remote server every day with i.e. a linked server and or via a SSIS package from your monitor server


Create the table 'Drives'.





Create the stored procedure 'sp_diskspace'.





Create the job '#dba - Drives'.






Back to articles          Next = How to check if the secondary node is readable in an AlwaysOn configuration?




   Ripat DBA          Contact